package gitee.derumin.really_forge.items;

import gitee.derumin.really_forge.enchantments.RFEnchantments;
import net.minecraft.block.AbstractGlassBlock;
import net.minecraft.block.BlockState;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.MiningToolItem;
import net.minecraft.item.ToolMaterial;
import net.minecraft.item.Vanishable;
import net.minecraft.registry.tag.BlockTags;

public class HammerItem extends MiningToolItem implements Vanishable , AvailableInForging {
    public HammerItem(ToolMaterial material, float attackDamage, float attackSpeed, Settings settings) {
        super(attackDamage, attackSpeed, material, BlockTags.ICE, settings);
    }

    public double getKnockBackPower(ItemStack stack){
        return (0.6
                *this.getMaterial().getAttackDamage()+1)/5
                *(1.0 + 0.1 * EnchantmentHelper.getLevel(RFEnchantments.HEAVINESS,stack))
                *(1.0 - 0.1 * EnchantmentHelper.getLevel(RFEnchantments.LITHE,stack));
    }

    @Override
    public float getMiningSpeedMultiplier(ItemStack stack, BlockState state) {
        if (state.getBlock() instanceof  AbstractGlassBlock || state.isIn(BlockTags.ICE)){
            return this.miningSpeed;
        }else if (state.isIn(BlockTags.PICKAXE_MINEABLE) || state.isIn(BlockTags.AXE_MINEABLE)){
            return 1+this.miningSpeed/2;
        }else return 1;
    }

    @Override
    public boolean postHit(ItemStack stack, LivingEntity target, LivingEntity attacker) {
        stack.damage(1, attacker, (e) -> e.sendEquipmentBreakStatus(EquipmentSlot.MAINHAND));
        return true;
    }

    @Override
    public boolean isSuitableFor(BlockState state) {
        int i = this.getMaterial().getMiningLevel();
        if (i < 3 && state.isIn(BlockTags.NEEDS_DIAMOND_TOOL)) {
            return false;
        } else if (i < 2 && state.isIn(BlockTags.NEEDS_IRON_TOOL)) {
            return false;
        } else {
            return (i >= 1 || !state.isIn(BlockTags.NEEDS_STONE_TOOL)) && (state.isIn(BlockTags.PICKAXE_MINEABLE) || state.isIn(BlockTags.AXE_MINEABLE));
        }
    }

    @Override
    public boolean isFireproof() {
        return this.getMaterial().equals(ExtraToolMaterial.NETHERITE_FOR_HAMMER)
                || super.isFireproof();
    }

    @Override
    public float getAnvilBreakPossibility(ItemStack stack) {
        return (float) (0.05 * this.getPower(stack));
    }

    @Override
    public float getPower(ItemStack stack) {
        return (float) ((this.getMaterial().getAttackDamage()+1)/5
                        * (1+0.2*EnchantmentHelper.getLevel(RFEnchantments.HEAVINESS,stack))
                        / (1+0.2*EnchantmentHelper.getLevel(RFEnchantments.LITHE,stack)));
    }

    @Override
    public boolean increasePunishment(float score, ItemStack stack) {
        return score < 0.2
                * (this.getMaterial().getAttackDamage()+1)/5
                * (1+0.2*EnchantmentHelper.getLevel(RFEnchantments.HEAVINESS,stack))
                * (1-0.2*EnchantmentHelper.getLevel(RFEnchantments.LITHE,stack));
    }
}
